package com.tfleo.mealssenger.adapter;

import java.util.List;

import com.github.ignition.core.widgets.RemoteImageView;
import com.tfleo.mealssenger.UserActivity;
import com.tfleo.mealssenger.R;
import com.tfleo.mealssenger.object.Comment;
import com.tfleo.mealssenger.object.Place;
import com.tfleo.mealssenger.object.News;
import com.tfleo.mealssenger.utility.PlaceIconFetcher;

import android.content.Context;
import android.content.Intent;
import android.location.Location;
import android.location.LocationManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.RatingBar;
import android.widget.TextView;

public class PlaceListAdapter extends ArrayAdapter<Place> {
	private LayoutInflater mInflater;
	private Context mContext;
	
	public PlaceListAdapter(Context context, int resource, int textViewResourceId, 
			List<Place> objects, LayoutInflater inflater) {
		super(context, resource, textViewResourceId, objects);
		mInflater = inflater;
		mContext = context;
	}
	
	@Override
    public View getView(int position, View convertView, ViewGroup parent) {   
		ViewHolder holder = null;
		TextView name = null;
		TextView distance = null;
		ImageView image = null;
		RatingBar rating = null;
		
		final Place p = getItem(position);

		if (convertView == null) {
			convertView = mInflater.inflate(R.layout.map_browse_list_entry, null);
			holder = new ViewHolder(convertView);
			convertView.setTag(holder);
		}
		
//		Location target = new Location(LocationManager.NETWORK_PROVIDER);
//		target.setLatitude(Float.valueOf(p.latitude));
//		target.setLongitude(Float.valueOf(p.longitude));
		
		holder = (ViewHolder) convertView.getTag();

		name = holder.getNameView();
		name.setText(p.name);
		
		distance = holder.getDistanceView();
		distance.setText(Integer.toString(p.getDistance()) + "m away");
		
		image = holder.getImageView();
		image.setImageResource(PlaceIconFetcher.getIcon(p.category));
		
		rating = holder.getRatingView();
		rating.setRating(p.rating);

		return convertView;
	}
	
	private class ViewHolder {
		private View vRow;
		private TextView vName = null;
		private TextView vDistance = null;
		private ImageView vImage = null;
		private RatingBar vRating = null;
		
		public ViewHolder(View row) {
			vRow = row;
		}

		public TextView getNameView() {
			if (vName == null) {
				vName = (TextView) vRow.findViewById(R.id.name);
			}
			return vName;
		}
		
		public TextView getDistanceView() {
			if(vDistance == null) {
				vDistance = (TextView) vRow.findViewById(R.id.distance);
			}
			return vDistance;
		}
		
		public ImageView getImageView() {
			if(vImage == null) {
				vImage = (ImageView) vRow.findViewById(R.id.img);
			}
			return vImage;
		}
		
		public RatingBar getRatingView() {
			if(vRating == null) {
				vRating = (RatingBar) vRow.findViewById(R.id.rating);
			}
			return vRating;
		}
	}
}
